package com.t2t.top.devops.controller;

import com.t2t.top.base.controller.BaseController;
import com.t2t.top.base.exception.BizException;
import com.t2t.top.base.exception.ServiceException;
import com.t2t.top.base.model.dto.ResponseDto;
import com.t2t.top.devops.constant.DevopsConstants;
import com.t2t.top.devops.constant.OperCodeConstants;
import com.t2t.top.devops.model.dto.AccountDto;
import com.t2t.top.devops.model.po.Account;
import com.t2t.top.devops.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yangpengfei
 */
@Controller
@RequestMapping(value = "/devops/account")
public class AccountController extends CommonController {

    protected Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping("/update")
    public String update(AccountDto account) throws BizException {
        try {
            accountService.update(account);
        } catch (BizException e) {
            ResponseDto responseDto = ResponseDto.bulidFailResult().setMsg(e.getMessage());
            getRequest().setAttribute("dto", responseDto);
            return "common/error";
        }
        return DevopsConstants.REDIRECT_URL + "account-list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Account po) throws BizException {
        if (po.getId() != null)  po = accountService.findById(po.getId());
        this.getRequest().setAttribute("po", po);
        return "account/update";
    }


    @RequestMapping("/query")
    @ResponseBody
    public ResponseDto query(AccountDto dto) throws BizException {
        List<Account> list = accountService.query(dto);
        return ResponseDto.bulidSuccessResult().setData(list);
    }


    @RequestMapping("/resetPswd")
    @ResponseBody
    public ResponseDto resetPswd(AccountDto dto) throws BizException {
        accountService.resetPswd(dto.getId());
        ResponseDto r = ResponseDto.bulidSuccessResult().setMsg("重置成功，密码已经修改为初始密码123456!");
        r.setData(Thread.currentThread().getStackTrace()[1].getMethodName());
        return r;
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResponseDto login(AccountDto dto) throws BizException {
        try {
            accountService.authe(dto.getName(), dto.getPassword());
            super.getRequest().getSession().setAttribute("AUTH_USER",dto);
            return ResponseDto.bulidSuccessResult().setMsg("登录成功!");
        } catch (Exception e) {
            logger.error("登录失败", e);
            return ResponseDto.bulidFailResult().setMsg(e.getMessage());
        }
    }

    @RequestMapping("/logout")
    @ResponseBody
    public ResponseDto logout(AccountDto dto) throws BizException {
        try {
            super.getRequest().getSession().removeAttribute("AUTH_USER");
            return ResponseDto.bulidSuccessResult().setMsg("登出成功!");
        } catch (Exception e) {
            logger.error("登录失败", e);
            return ResponseDto.bulidFailResult().setMsg(e.getMessage());
        }
    }

    @RequestMapping("/changePswd")
    @ResponseBody
    public ResponseDto changePswd(AccountDto dto) throws BizException {
        try {
            AccountDto account = getAccount();
            if(account == null) throw new ServiceException("请重新登录");

            accountService.changePswd(account.getName(),dto.getOldPswd(),dto.getNewPswd());
            super.getRequest().getSession().removeAttribute("AUTH_USER");
            return ResponseDto.bulidSuccessResult().setMsg("修改成功!");
        } catch (Exception e) {
            logger.error("修改失败", e);
            return ResponseDto.bulidFailResult().setMsg(e.getMessage());
        }
    }


    @RequestMapping("/portal")
    public String portal(AccountDto dto) throws BizException {
        return "iframe/main";
    }

}
